using System;
using UnityEditor;
using UnityEngine;

namespace UnityXEditor
{
    [Serializable]
    public class LogSettings
    {
        [Flags]
        public enum LogType
        {
            无 = 0,
            调试信息 = 1,
            警告信息 = 2,
            错误信息 = 4,
            事件信息 = 8,
            全部 = -1,
        }

        [SerializeField]
        private LogType m_EnabledLogType = LogType.全部;

        [SerializeField]
        private bool m_EnabledConsole = false;
        [SerializeField]
        private bool m_EnabledFile = false;
        [SerializeField]
        private bool m_EnabledDefaultOpen = false;

        public LogType enabledLogType
        {
            get { return m_EnabledLogType; }
            set
            {
                if (value != m_EnabledLogType)
                {
                    m_EnabledLogType = value;
                    Update();
                }
            }
        }
        public bool enabledConsole
        {
            get { return m_EnabledConsole; }
            set
            {
                if (value != m_EnabledConsole)
                {
                    m_EnabledConsole = value;
                    Update();
                }
            }
        }
        public bool enabledFile
        {
            get { return m_EnabledFile; }
            set
            {
                if (value != m_EnabledFile)
                {
                    m_EnabledFile = value;
                    Update();
                }
            }
        }
        public bool enabledDefaultOpen
        {
            get { return m_EnabledDefaultOpen; }
            set
            {
                if (value != m_EnabledDefaultOpen)
                {
                    m_EnabledDefaultOpen = value;
                    Update();
                }
            }
        }
        public void OnGUI()
        {
            FontStyle origFontStyle = EditorStyles.label.fontStyle;
            EditorStyles.label.fontStyle = FontStyle.Bold;
            EditorGUILayout.LabelField("日志", EditorStyles.boldLabel);
            EditorStyles.label.fontStyle = origFontStyle;
            EditorGUI.indentLevel++;
            {
                m_EnabledLogType = (LogType)EditorGUILayout.EnumFlagsField("启用日志类型", m_EnabledLogType);
                m_EnabledFile = EditorGUILayout.Toggle("启用日志文件", m_EnabledFile);
                m_EnabledConsole = EditorGUILayout.Toggle("启用控制台窗口", m_EnabledConsole);
                EditorGUI.BeginDisabledGroup(!m_EnabledConsole);
                EditorGUI.indentLevel++;
                {
                    m_EnabledDefaultOpen = EditorGUILayout.Toggle("添加默认打开方式（三击屏幕左下角 或 按键F1+F12）", m_EnabledDefaultOpen);
                }
                EditorGUI.indentLevel--;
                EditorGUI.EndDisabledGroup();
            }
            EditorGUI.indentLevel--;
        }
        public void Update()
        {
            CodeFileTool.CommentsLine("Log.cs", "#define ENABLED_DEBUG", (m_EnabledLogType & LogType.调试信息) == LogType.调试信息);
            CodeFileTool.CommentsLine("Log.cs", "#define ENABLED_WARNING", (m_EnabledLogType & LogType.警告信息) == LogType.警告信息);
            CodeFileTool.CommentsLine("Log.cs", "#define ENABLED_ERROR", (m_EnabledLogType & LogType.错误信息) == LogType.错误信息);
            CodeFileTool.CommentsLine("Log.cs", "#define ENABLED_EVENT", (m_EnabledLogType & LogType.事件信息) == LogType.事件信息);
            CodeFileTool.CommentsLine("Log.cs", "#define ENABLED_CONSOLE", m_EnabledConsole);
            CodeFileTool.CommentsLine("Log.cs", "#define ENABLED_FILE", m_EnabledFile);

            CodeFileTool.CommentsLine("ConsoleDefaultOpen.cs", "#define ENABLED", m_EnabledConsole && m_EnabledDefaultOpen);
        }
    }
}